/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.util.Stack;

import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JFrame;

import org.apache.log4j.Logger;

import who.tmap.common.Constants;
import who.tmap.util.MiscHelper;

import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class JFrameHelp extends JFrame {

    private static Logger logger = Logger.getLogger(JFrameHelp.class);	
	
	private JPanel jContentPane = null;
	private JEditorPane jEditorPaneHelp = null;
	private JScrollPane jScrollPane = null;
	private JPanel jPanelButton = null;
	private JButton jButtonClose = null;

	/**
	 * This is the default constructor
	 */
	public JFrameHelp() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 400);
		this.setContentPane(getJContentPane());
		this.setTitle("TMap Designer Help");
		this.setLocationRelativeTo(null);
		setIconImage( new MiscHelper().getImageIcon( "ic_tmapdesigner.gif" ).getImage() );	
		
		getRootPane().setDefaultButton( getJButtonClose() );
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
			jContentPane.add(getJPanelButton(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJEditorPaneHelp());
		}
		return jScrollPane;
	}
	
	/**
	 * This method initializes jTextPaneHelp	
	 * 	
	 * @return javax.swing.JTextPane	
	 */
	private JEditorPane getJEditorPaneHelp() {
		if (jEditorPaneHelp == null) {
			jEditorPaneHelp = new JEditorPane();
			jEditorPaneHelp.setEditable( false );
			URL url = getClass().getResource(Constants.HELP_FILE);			
			jEditorPaneHelp.addHyperlinkListener( new HelpHyperlinkListener( url ) );

			try {
				jEditorPaneHelp.setPage(url);
			} catch (IOException e) {
				logger.error( e );
			}
			jEditorPaneHelp.setEditable(false);
		}
		return jEditorPaneHelp;
	}

	/**
	 * This method initializes jPanelButton	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelButton() {
		if (jPanelButton == null) {
			jPanelButton = new JPanel();
			jPanelButton.add(getJButtonClose(), null);
		}
		return jPanelButton;
	}

	/**
	 * This method initializes jButtonClose	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonClose() {
		if (jButtonClose == null) {
			jButtonClose = new JButton();
			jButtonClose.setText("Close");
			jButtonClose.addActionListener(new ActionListener() {
				public void actionPerformed( ActionEvent e) {
					setVisible( false );
				}
			});
		}
		return jButtonClose;
	}
	
    private class HelpHyperlinkListener implements HyperlinkListener {
    	private static final String BACK ="javascript:history.back();";
    	private static final String TOP="#top";
    	// To implement history.back as javascript does not work into JEditorPane
    	private Stack<URL> urls =new Stack<URL>();
    	private URL startUrl;
    	
    	public HelpHyperlinkListener( URL url ) {
    		super();
    		urls.push( url );
    		startUrl =url;
    	}
    	
        public void hyperlinkUpdate(HyperlinkEvent evt) {
            if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane pane = (JEditorPane)evt.getSource();
                try {
                	URL url;

                	String desc =evt.getDescription();
                	if ( desc.equalsIgnoreCase( TOP ) ) {
                		pane.setCaretPosition( 0 );
	                } else {
	                	if ( desc.equalsIgnoreCase( BACK ) ) {
	                		if ( !urls.isEmpty() ) {
	                			url =urls.pop();
	                		} else {
	                			url =startUrl;
	                		}
	                	} else {
	                		urls.push( pane.getPage() );
	                		url =evt.getURL();
	                	}
	                	
	                    // Show the new page in the editor pane.
	                    pane.setPage(url);
                	}
                } catch (IOException e) {
                }
            }
        }
    }		

}


